dojo.declare("MVendorOrderCurrentPg", wm.Page, {
    start: function() {

        this.reloadMyOrders();
    },
    "preferredDevice": "phone",

    reloadMyOrders: function() {
        var mainPage = wm.Page.getPage("MobileMainPg");

        // this.svTodaysOrders.input.setValue("vendorId", mainPage.vUserId);
        this.svOrders.input.setValue("vendorId", mainPage.vUserId);
        this.svOrders.update();

    },
    list1OrderIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var comment = '';
        if (rowObj.comments !== null) comment = rowObj.comments;

        var display = "<div class='MobileRowTitle'><b>Order#:</b> " + rowObj.orderId + "</div>" + "<div class='MobileRow'>" + wm.List.prototype.dateFormatter({
            "formatLength": "medium",
            "dateType": "date and time"
        }, null, null, null, rowObj.orderDt) + "</div>" +

        "<div class='MobileRow'><b> TotalPrice: " + rowObj.totalPrice + "</b></div>" + "<div class='MobileRow'> Customer:" + rowObj.customerByCustomerId.name + "</div>" + "<div class='MobileRow'> Ph:" + rowObj.customerByCustomerId.primaryPhone + "</div>" + "<div class='MobileRow'> Address: Block " + rowObj.customerByCustomerId.block.blockName + ' Door ' + rowObj.customerByCustomerId.door.doorName + "</div>" + "<div class='MobileRow'> Status:" + rowObj.status + "</div>" + "<div class='MobileRow'> Comments:" + comment + "</div>";
        return display;
    },
    list1StatusFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'><b>Status :</b> </br> " + rowObj.status + "</div>";
        return display;
    },
    svUpdateOrderStatusSuccess1: function(inSender, inDeprecated) {
        this.lvMyOrders.clearData();
        this.lvMyOrders.update();
    },
    buttonDeliveredClick: function(inSender) {
        if (this.smOrderStatus.getDataValue()) {
            this.svUpdateOrderStatus.update();
        }
    },
    list1Select2: function(inSender, inItem) {
        //  this.smOrderStatus.setDataValue("Delivered");
        wm.Page.getPage("MVendorHomePg").vPageBack = "NEW_ORDERS_ITEMS";
        this.largeTextArea1.clear();
    },
    listVendorOrderSummaryFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var comment = "";
        if (rowObj.comment) {
            comment = rowObj.comment === null ? "" : rowObj.comment;
        }
        var vendor_comment = "";
        if (rowObj.vendor_comment) {
            vendor_comment = rowObj.vendor_comment === null ? "" : rowObj.vendor_comment;
        }

        var blkName = rowObj.blockName === null ? "" : rowObj.blockName;
        var doorName = rowObj.doorName === null ? "" : rowObj.doorName;
        var display = "<div style='display:block;font-weight: normal;'>Order#:<b> " + rowObj.orderId + "</b></div>" +

        "<div class='MobileRowTitle'>" + wm.List.prototype.dateFormatter({
            "formatLength": "medium",
            "dateType": "date and time"
        }, null, null, null, rowObj.orderDt) + "</div>" +

        "<div style='display:block;font-weight: normal;'> TotalPrice: <b>" + rowObj.price + " Rs</b></div>" + "<div style='display:block;font-weight: normal;'> Customer:<b>" + rowObj.customerName + "</b></div>" + 
        "<div style='display:block;font-weight: normal;'> Ph:<b> <a href='tel:"+rowObj.contactNum+"'>" + rowObj.contactNum + "</a>"+
        //    <a href='tel:080-25723322'>Customer Care 080-25723322</a>
        "</b></div>" + "<div style='display:block;font-weight: normal;'> Address:<b>" + 
        rowObj.address + '</b> ' + "<div style='display:block;font-weight: normal;'> Status:<b>" + 
        rowObj.status + "</b></div>" + "<div style='display:block;font-weight: normal;'>Customer Comments:<b>" + comment + 
        "</b></div>" + "<div style='display:block;font-weight: normal;'>Vendor Comments:<b>" + vendor_comment + "</b></div>";


        return display;
    },
    listVendorCurrentOrdersFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        var display = "<div style='display:block;font-weight: normal;'>Order#:<b> " + rowObj.orderId + "</b></div>" +

        "<div class='MobileRowTitle'>" + wm.List.prototype.dateFormatter({
            "formatLength": "medium",
            "dateType": "date and time"
        }, null, null, null, rowObj.orderDt) + "</div>" + "<div style='display:block;font-weight: normal;'> TotalPrice: <b>" + rowObj.totalPrice + " Rs</b></div>" + "<div style='display:block;font-weight: normal;'> Customer:<b>" + rowObj.customerName + "</b></div>" + "<div style='display:block;font-weight: normal;'> Ph:<b> <a href='tel:" + rowObj.contactNum + "'>" + rowObj.contactNum + "</a></b></div>" + "<div style='display:block;font-weight: normal;'> Address:" + rowObj.orderAddress + "</div>" + "<div style='display:block;font-weight: normal;'> Status:" + rowObj.status + "</div>" + "<br><div style='display:block;font-weight: normal;'> Comments:" + rowObj.comment + "</div>"; // + "<div style='display:block;font-weight: normal;'>" + rowObj.itemDetails + "</div>";
        return display;
    },
    list2StatusFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        // console.error('rowObj.status==' + rowObj.status);
        if (rowObj.status == 'Delivered') {

            var buttonText = '<img src ="resources/images/mobile/OK.png" height="16px" width="16px" /> <br> Delivered';
            return buttonText;
        } else {

            return (rowObj.status === null ? "" : rowObj.status);
        }
    },
    list2Select: function(inSender, inItem) {

        var itemId = this.list2.selectedItem.data.orderItemId;

        var itemStatus = this.list2.selectedItem.data.status;

        if (itemStatus && itemStatus === "Delivered") {

            this.svUpdateOrderItemStatus.input.setValue("status", 'NEW');
            this.list2.setCell(this.list2.getSelectedIndex(), "status", 'NEW', false);


        } else {


            this.svUpdateOrderItemStatus.input.setValue("status", 'Delivered');
            this.list2.setCell(this.list2.getSelectedIndex(), "status", 'Delivered', false);
        }
        this.svUpdateOrderItemStatus.input.setValue("orderItemId", itemId);

        //this.svUpdateOrderItemStatus.update();
        this.list2.deselectAll();
    },
    svUpdateOrderItemStatusResult: function(inSender, inDeprecated) {
        this.list2.deselectAll();

    },
    list2OrderItemIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var comments = rowObj.comments === null ? "" : rowObj.comments;

        var unit = rowObj.items.unit;

        if (!unit || unit === null) {
            unit = "";
        }
        var display = "<div style='display:block;font-weight: normal;'>Item Name: <b>" + rowObj.items.itemName + "</b></div>" + "<div style='display:block;font-weight: normal;'>Quantity: <b>" + rowObj.quantity + unit + "</b></div>" + "<div style='display:block;font-weight: normal;'>Price: <b>" + rowObj.price + " Rs</b></div>";
        //+       "<div style='display:block;font-weight: normal;'>Comments: " + comments + "</div>";
        return display;
    },
    svTodaysOrdersResult: function(inSender, inDeprecated) {
        if (this.svTodaysOrders.getCount() > 0) {
            this.panel2.setShowing(false);
            // this.buttonNoOrders.setShowing(false);
        } else {
            this.panel2.setShowing(true);
            // this.buttonNoOrders.setShowing(true);
        }
    },
    svUpdateOrderStatusError: function(inSender, inError) {

    },
    svUpdateOrderItemStatusError: function(inSender, inError) {

    },
    lvOrderItemsSuccess: function(inSender, inDeprecated) {
        //this.list2.cle
    },
    listCurrentOrdersVendorNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var buttonText = '<img src ="resources/images/mobile/next_img.png" height="26px" />';
        return buttonText;
    },
    svUpdateOrderStatusBkError: function(inSender, inError) {
        this.svUpdateOrderStatusError(inSender, inError);
    },
    svUpdateOrderStatusAllError: function(inSender, inError) {

    },
    bCompleteOrder1Click: function(inSender) {
        //
        var selItemIds = "";
        var isAdd = false;
        for (var count = 0; count < this.list2.getCount(); count++) {

            var rowObj = this.list2.getItemData(count);

            if (rowObj.status === "Delivered") {
                if (isAdd) {
                    selItemIds = selItemIds + ",";
                }
                selItemIds = selItemIds + rowObj.orderItemId;
                isAdd = true;

            }

        }
        if (selItemIds !== "") {
            this.svUpdatePartialOrderStatus.input.setValue("itemIds", selItemIds);
            this.svUpdatePartialOrderStatus.input.setValue("orderComment", this.largeTextArea1.getDisplayValue());
            this.svUpdatePartialOrderStatus.update();
        } else {
            this.layerOrder.activate();
        }
    },


    svOrdersError: function(inSender, inError) {

    },
    svOrdersResult: function(inSender, inDeprecated) {

    },
    bCompleteOrderClick: function(inSender) {

        this.svUpdateOrderStatusAll.input.setValue("orderComment", this.largeTextArea1.getDisplayValue());
        this.svUpdateOrderStatusAll.update();
    },
    _end: 0
});